# _*_ coding:utf-8 _*_
"""
@author:lx
@file: calchist
@contact: lixiang-929@outlook.com
@time: 2019/03/19
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time

image = cv2.imread('./data/1.png')
HSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
H = HSV[:,:,0]
S = HSV[:,:,1]
V = HSV[:,:,2]

start_time = time.time()
# hist = cv2.calcHist([img],[0],None,[256],[0,256])
image_width = image.shape[1]
image_height = image.shape[0]
b_channel, g_channel, r_channel = cv2.split(image)

rg_image = np.zeros([image_height, image_width])
rb_image = np.zeros([image_height, image_width])
gb_image = np.zeros([image_height, image_width])
gr_image = np.zeros([image_height, image_width])
fR = np.zeros([image_height, image_width])
fY = np.zeros([image_height, image_width])
# Fxy = np.zeros([image_height,image_width])

for j in range(image_height):
    for i in range(image_width):

        rg_image[j, i] = int(r_channel[j, i]) - int(g_channel[j, i])
        rb_image[j, i] = int(r_channel[j, i]) - int(b_channel[j, i])
        gr_image[j, i] = int(g_channel[j, i]) - int(r_channel[j, i])
        gb_image[j, i] = int(g_channel[j, i]) - int(b_channel[j, i])

# for j in range(image_height):
#     for i in range(image_width):
#
        fR[j, i] = max(0,min(rg_image[j, i],rb_image[j, i]))
        fY[j, i] = max(0,min(rb_image[j, i],gb_image[j, i]))
        # Fxy[j, i] = fR[j, i] + fY[j, i]

cv2.imwrite('fR.jpg', fR)
cv2.imwrite('fY.jpg', fY)
# cv2.imwrite('Fxy.jpg', Fxy)
print("{0:.3f}, {1:s}".format((time.time() - start_time), '4fxy'))
# plt.hist(image.ravel(),256,[0,256])
# plt.hist(rg_image.ravel(),256,[0,256])
# plt.hist(gb_image.ravel(),256,[0,256])
# plt.show()
